﻿#include <sstream>
#include "zyPropertyGrid.h"

DarkMoonUI::zyPropertyGrid::zyPropertyGrid()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyPropertyGrid_WndProc);
	m_obj->setChildWndProc(zyPropertyGrid_ChildWndProc);
	m_obj->setDestroy(zyPropertyGrid_Destroy);
}
DarkMoonUI::zyPropertyGrid::~zyPropertyGrid()
{

}
void WINAPI DarkMoonUI::zyPropertyGrid::zyPropertyGrid_Destroy(zyObject* obj)
{
	zyPropertyGrid* zyb = (zyPropertyGrid*)obj->getPtrData(L"this");
	zyb->OnNCDestroy();
	zyCtrol::zyCtrol_Destroy(obj);
}
LRESULT WINAPI DarkMoonUI::zyPropertyGrid::zyPropertyGrid_ChildWndProc(zyObject* obj, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	if (message == WM_NOTIFY) {
		NMHDR* nm = reinterpret_cast<NMHDR*>(lParam);
		switch (nm->code) {
		case NM_CUSTOMDRAW: {
			NMLVCUSTOMDRAW* nmlvCust = reinterpret_cast<NMLVCUSTOMDRAW*>(lParam);
			switch (nmlvCust->nmcd.dwDrawStage) {
			case CDDS_PREPAINT:
				*bHandle = TRUE;
				return CDRF_NOTIFYITEMDRAW;

			case CDDS_ITEMPREPAINT: {
				PropertyGrid_item* pItem = reinterpret_cast<PropertyGrid_item*>(nmlvCust->nmcd.lItemlParam);
				if (pItem) {
					zyPropertyGrid* p = reinterpret_cast<zyPropertyGrid*>(obj->getPtrData(L"this"));
					p->propertyItem_OnDraw(pItem, lParam);
				}
				*bHandle = TRUE;
				return CDRF_SKIPDEFAULT;
			}

			default:
				*bHandle = TRUE;
				return CDRF_DODEFAULT;
			}
		}

		case LVN_ITEMCHANGED: {
			NMLISTVIEW* nmlv = reinterpret_cast<NMLISTVIEW*>(lParam);
			if ((nmlv->uNewState & LVIS_SELECTED) == LVIS_SELECTED) {
				PropertyGrid_item* pItem = reinterpret_cast<PropertyGrid_item*>(nmlv->lParam);
				if (pItem && pItem->Type != 属性类型_分组) { // 跳过分组项
					zyPropertyGrid* p = reinterpret_cast<zyPropertyGrid*>(obj->getPtrData(L"this"));
					p->propertyItem_BeginEdit(pItem, nmlv->iItem);
				}
			}
			break;
		}

		case LVN_DELETEITEM: {
			zyPropertyGrid* p = reinterpret_cast<zyPropertyGrid*>(obj->getPtrData(L"this"));
			p->zyPropretyGrid_HideAll();
			break;
		}

		default:
			break;
		}
	}
	return zyCtrol::zyCtrol_ChildWndProc(obj, hWnd, message, wParam, lParam, bHandle);
}
LRESULT WINAPI DarkMoonUI::zyPropertyGrid::zyPropertyGrid_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	if (uMsg == WM_NCDESTROY) {
		zyPropertyGrid* zyb = (zyPropertyGrid*)obj->getPtrData(L"this");
		zyb->OnNCDestroy();
	}
	else if (uMsg == WM_HSCROLL || uMsg == WM_VSCROLL) {
		zyPropertyGrid* p = (zyPropertyGrid*)obj->getPtrData(L"this");
		p->zyPropretyGrid_HideAll();
	}
	else if (uMsg == WM_SIZE) {
		zyPropertyGrid* p = (zyPropertyGrid*)obj->getPtrData(L"this");
		p->zyPropretyGrid_HideAll();
		int NameWidth = (int)SendMessage(hWnd, LVM_GETCOLUMNWIDTH, 0, 0);
		int valueWidth = LOWORD(lParam) - NameWidth;
		SendMessage(hWnd, LVM_SETCOLUMNWIDTH, 1, valueWidth);
	}
	else if (uMsg == WM_MOUSEMOVE ) {
		RECT rc = { 0 };
		GetClientRect(hWnd, &rc);
		int NameWidth = (int)SendMessage(hWnd, LVM_GETCOLUMNWIDTH, 0, 0);
		rc.left = NameWidth - 5;
		rc.right = NameWidth + 5;
		if (PtInRect(&rc, POINT{ LOWORD(lParam), HIWORD(lParam) }) != 0) {
			HCURSOR hCursor = LoadCursor(0, IDC_SIZEWE);
			SetCursor(hCursor);
		}
		if (obj->hasPtrData(L"DrapX")) {
			RECT rc = { 0 };
			GetClientRect(hWnd, &rc);
			int x =(int)(LONG_PTR)obj->getPtrData(L"DrapX");
			obj->setPtrData(L"DrapX", (void*)LOWORD(lParam));
			x = x - LOWORD(lParam);
			NameWidth = (int)SendMessage(hWnd, LVM_GETCOLUMNWIDTH, 0, 0);
			NameWidth = NameWidth - x;
			int valueWidth = rc.right - rc.left - NameWidth;
			if (NameWidth < 50 || valueWidth < 50) {
				obj->removePtrData(L"DrapX");
				ReleaseCapture();
				return zyCtrol::zyCtrol_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandle);
			}
			if (x > 0) {
				SendMessage(hWnd, LVM_SETCOLUMNWIDTH, 0, NameWidth);
				SendMessage(hWnd, LVM_SETCOLUMNWIDTH, 1, valueWidth);
			}
			else {
				SendMessage(hWnd, LVM_SETCOLUMNWIDTH, 1, valueWidth);
				SendMessage(hWnd, LVM_SETCOLUMNWIDTH, 0, NameWidth);
			}
		}
	}
	else if (uMsg == WM_LBUTTONDOWN) {
		RECT rc = { 0 };
		GetClientRect(hWnd, &rc);
		int NameWidth = (int)SendMessage(hWnd, LVM_GETCOLUMNWIDTH, 0, 0);
		rc.left = NameWidth - 5;
		rc.right = NameWidth + 5;
		zyPropertyGrid* p = (zyPropertyGrid*)obj->getPtrData(L"this");
		if (PtInRect(&rc, POINT{ LOWORD(lParam), HIWORD(lParam) }) != 0) {
			p->zyPropretyGrid_HideAll();
			HCURSOR hCursor = LoadCursor(0, IDC_SIZEWE);
			SetCursor(hCursor);
			obj->setPtrData(L"DrapX", (void*)LOWORD(lParam));
			SetCapture(hWnd);
			*bHandle = TRUE;
			return 0;
		}
		LRESULT ret = p->zyPropretyGrid_OnItemClick(hWnd, LOWORD(lParam), HIWORD(lParam), 0, bHandle);
		if (*bHandle)
			return ret;
	}
	else if (uMsg == WM_LBUTTONUP) {
		RECT rc = { 0 };
		GetClientRect(hWnd, &rc);
		int NameWidth = (int)SendMessage(hWnd, LVM_GETCOLUMNWIDTH, 0, 0);
		rc.left = NameWidth - 5;
		rc.right = NameWidth + 5;
		if (PtInRect(&rc, POINT{ LOWORD(lParam), HIWORD(lParam) }) != 0) {
			HCURSOR hCursor = LoadCursor(0, IDC_SIZEWE);
			SetCursor(hCursor);
		}
		if (obj->hasPtrData(L"DrapX")) {
			obj->removePtrData(L"DrapX");
			ReleaseCapture();
		}
	}
	else if (uMsg == WM_LBUTTONDBLCLK) {
		zyPropertyGrid* p = (zyPropertyGrid*)obj->getPtrData(L"this");
		LRESULT ret = p->zyPropretyGrid_OnItemClick(hWnd, LOWORD(lParam), HIWORD(lParam), 1, bHandle);
		if (*bHandle)
			return ret;
	}
	return zyCtrol::zyCtrol_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandle);
}
void DarkMoonUI::zyPropertyGrid::OnNCDestroy() {
	zyLmageList* pImageList = (zyLmageList*)GetMember(L"pImageList");
	if (pImageList != nullptr) {
		//IUnknown_Release (pImageList);
		pImageList->getObject()->release();
		delete pImageList;
		SetMember(L"pImageList", 0);
	}
	std::vector<PropertyGrid_item*>* Propertys = (std::vector<PropertyGrid_item*>*)GetMember(L"Propertys");
	if (Propertys != nullptr) {
		for (int i = 0; i < Propertys->size(); i++) {
			PropertyGrid_item* value = (*Propertys)[i];
			if (value != nullptr) {
				//IUnknown_Release (value);
				//((zyObject*)value)->release();
				delete value;
			}
		}
		delete Propertys;
		SetMember(L"Propertys", 0);
	}
	zyEditBox* pEdit = (zyEditBox*)GetMember(L"pEdit");
	if (pEdit != nullptr) {
		//IUnknown_Release (pEdit);
		pEdit->getObject()->release();
		delete pEdit;
		SetMember(L"pEdit", 0);
	}
	zyListBox* pList = (zyListBox*)GetMember(L"pList");
	if (pList != nullptr) {
		//IUnknown_Release (pEdit);
		pList->getObject()->release();
		delete pList;
		SetMember(L"pList", 0);
	}
	zyWidget* pListWindow = (zyWidget*)GetMember(L"pListWindow");
	if (pListWindow != nullptr) {
		//IUnknown_Release (pEdit);
		pListWindow->getObject()->release();
		delete pListWindow;
		SetMember(L"pListWindow", 0);
	}
}

LRESULT DarkMoonUI::zyPropertyGrid::zyPropretyGrid_OnItemClick(HWND hWnd, int x, int y, int mouseState, BOOL* bHandle) {
	LVHITTESTINFO lvht = { {x, y}, 0, 0, 0 };
	SendMessage(hWnd, LVM_SUBITEMHITTEST, 0, (LPARAM)&lvht);

	if (lvht.iItem != -1) {
		LVITEM item = { 0 };
		item.mask = LVIF_PARAM;
		item.iItem = lvht.iItem;
		SendMessage(hWnd, LVM_GETITEMW, 0, (LPARAM)&item);

		PropertyGrid_item* pItem = reinterpret_cast<PropertyGrid_item*>(item.lParam);
		if (pItem != nullptr) {
			int type = pItem->Type;
			int level = pItem->Level;

			// 处理分组类型的点击
			if (type == 属性类型_分组) {
				zyPropretyGrid_ExpandChange(pItem, lvht.iItem);
				return 0;
			}

			// 处理非分组类型的点击
			RECT rc = { 0 };
			SendMessage(hWnd, LVM_GETITEMRECT, lvht.iItem, (LPARAM)&rc);
			rc.left = (level - 1) * 16;
			rc.right = rc.left + 16;

			// 检查是否点击了展开/折叠区域
			if (PtInRect(&rc, lvht.pt)) {
				zyPropretyGrid_ExpandChange(pItem, lvht.iItem);
				return 0;
			}

			// 处理子项的点击
			rc.top = 1;
			SendMessage(hWnd, LVM_GETSUBITEMRECT, lvht.iItem, (LPARAM)&rc);
			if (PtInRect(&rc, lvht.pt)) {
				rc.left = rc.right - (rc.bottom - rc.top);
				// 处理鼠标按下事件
				if (mouseState == 0) {
					int selected = (int)SendMessage(hWnd, LVM_GETITEMSTATE, lvht.iItem, LVIS_SELECTED);
					if (selected == LVIS_SELECTED) {
						*bHandle = TRUE;
					}
				}

				// 调用点击事件处理函数
				propertyItem_OnClick(pItem, lvht.iItem, PtInRect(&rc, lvht.pt), mouseState);
				return 0;
			}
		}
	}
	return 0;
}
void DarkMoonUI::zyPropertyGrid::zyPropretyGrid_ExpandChange(PropertyGrid_item* pItem, int index) {
	int state = pItem->State;
	if ((state & 属性状态_只读) != 0) {
		return;
	}
	zyPropretyGrid_HideAll();
	std::vector<PropertyGrid_item*>* childs = pItem->Childs;
	HWND hWnd = (HWND)GetMember(L"hWnd");
	if (childs == nullptr) return;
	int count = (int)childs->size();
	if (count == 0) return;

	if ((state & 属性状态_扩展) != 0) {
		// 收缩状态：删除所有子项
		state &= ~属性状态_扩展;
		for (int i = count - 1; i >= 0; i--) {  // 逆序删除防止索引变化
			LVITEM item = { 0 };
			item.mask = LVIF_PARAM;
			item.iItem = index + 1; // 始终保持删除第一个子项
			if (SendMessage(hWnd, LVM_GETITEM, 0, (LPARAM)&item)) {
				if (item.lParam != 0) {
					PropertyGrid_item* child = (PropertyGrid_item*)item.lParam;
					if (child->Parent == pItem) {
						SendMessage(hWnd, LVM_DELETEITEM, index + 1, 0);
					}
				}
			}
		}
		// 简化版代码
		//state &= ~属性状态_扩展;
		//for (int i = 0; i < count; i++) {
		//	SendMessage(hWnd, LVM_DELETEITEM, index + 1, 0);
		//}
		// 强制重绘父项及周围区域
		//InvalidateRect(hWnd, NULL, TRUE);
	}
	else {
		// 扩展状态：插入子项
		state |= 属性状态_扩展;
		for (int i = 0; i < count; i++) {
			LVITEM lvitem = { 0 };
			lvitem.mask = LVIF_PARAM | LVIF_STATE | LVIF_TEXT;
			lvitem.iItem = index + 1 + i; // 插入到父项之后
			lvitem.iSubItem = 0;
			lvitem.state = 0;
			lvitem.stateMask = LVIS_STATEIMAGEMASK;
			PropertyGrid_item* child = (*childs)[i];
			lvitem.pszText = (LPWSTR)child->Name.c_str(); // 使用子项名称
			lvitem.cchTextMax = (int)child->Name.size();
			lvitem.lParam = (LPARAM)child;
			SendMessage(hWnd, LVM_INSERTITEM, 0, (LPARAM)&lvitem);
		}
	}
	pItem->State = state;
	SendMessage(hWnd, LVM_REDRAWITEMS, index, index);//不知道为什么这个刷新导致有绘制残留的背景.所以用下面的强制重绘  添加了LVIF_STATE标识刷新正常
	//InvalidateRect(hWnd, NULL, TRUE);// 强制重绘父项及周围区域
}
void DarkMoonUI::zyPropertyGrid::zyPropretyGrid_HideAll() {
	zyEditBox* pCtrol = (zyEditBox*)GetMember(L"pEdit");
	if (pCtrol != nullptr) {
		HWND hWnd = (HWND)pCtrol->GetMember(L"hWnd");
		if (IsWindowVisible(hWnd) != 0) {
			ShowWindow(hWnd, SW_HIDE);
		}
	}
	zyWidget *pCtrol1 = (zyWidget*)GetMember(L"pListWindow");
	if (pCtrol1 != nullptr) {
		HWND hWnd = (HWND)pCtrol1->GetMember(L"hWnd");
		if (IsWindowVisible(hWnd) != 0) {
			ShowWindow(hWnd, SW_HIDE);
		}
	}
}

LRESULT  DarkMoonUI::zyPropertyGrid::propertyItem_Edit_Msg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL* bHandle) {
	if (msg == WM_SHOWWINDOW) {
		if (wParam == 0) { // 被隐藏{
			HANDLE pItem = GetProp(hWnd, L"pItem");
			HANDLE pListView = GetProp(hWnd, L"pListView");
			if(pItem != 0 && pListView != 0){
				propertyItem_EndEdit (pListView, pItem, -1) ; // OnEndEdit;
			} 
		}
	}
	else if (msg == WM_KILLFOCUS) 
		ShowWindow(hWnd, SW_HIDE);
	return 0;
}
LRESULT  DarkMoonUI::zyPropertyGrid::propertyItem_list_Msg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL* bHandle) {
	if (msg == WM_MOUSEMOVE) {
		int iItem = LOWORD(SendMessage(hWnd, LB_ITEMFROMPOINT, 0, lParam));
		if (iItem != -1) { 
			int curentItem = (int)SendMessage(hWnd, LB_GETCURSEL, 0, lParam);
			if (curentItem != iItem) {
				SetFocus(hWnd);
				SendMessage(hWnd, LB_SETCURSEL, iItem, 0);
			}
		}
	}
	else if (msg == WM_KILLFOCUS) {
		ShowWindow(GetParent(hWnd), SW_HIDE);
		HANDLE pListView = GetProp(hWnd, L"pListView");
		if (pListView != 0) {
			HWND hWndLV = (HWND)GetMember(L"hWnd");
			LVHITTESTINFO lvht = { 0 };
			POINT pt = { 0 };
			GetCursorPos(&pt);
			lvht.pt = pt;
			SendMessage(hWndLV, LVM_SUBITEMHITTEST, 0, (LPARAM)& lvht);
			if (lvht.iItem != -1 && lvht.iSubItem == 1) {
				if (SendMessage(hWndLV, LVM_GETITEMSTATE, lvht.iItem, LVIS_SELECTED) == LVIS_SELECTED) 
					SetProp(hWnd, L"NoShow", (HANDLE)1);
				else
					SetProp(hWnd, L"NoShow", (HANDLE)0);
			}
			else
				SetProp(hWnd, L"NoShow", (HANDLE)0);
		}
	}
	return 0;
}
int DarkMoonUI::zyPropertyGrid::propertyItem_List_OnSelected(HWND hWnd, int ID) {
	int index = (int)SendMessage(hWnd, LB_GETCURSEL, 0, 0);
	ShowWindow(GetParent(hWnd), SW_HIDE);
	PropertyGrid_item* pItem = (PropertyGrid_item*)GetProp(hWnd, L"pItem");
	HANDLE pListView = GetProp(hWnd, L"pListView");
	if (pItem != nullptr && pListView != 0) {
		propertyItem_EndEdit(pListView, pItem, index);
	}
	return 0;
}
int DarkMoonUI::zyPropertyGrid::propertyItem_LongText_Dialog_OnCraete(zyForm* form) {
	HWND hWnd = form->getHwnd();
	SendMessage(hWnd, WM_SETICON, 0, 0);
	SendMessage(hWnd, WM_SETICON, 1, 0);
	zyEditBox* LongText_Edit = new zyEditBox();
	LongText_Edit->创建(hWnd, L"", POINT{0,0}, POINT{ 0,0 }, 0, ES_AUTOVSCROLL | ES_WANTRETURN | ES_MULTILINE | WS_TABSTOP | WS_VSCROLL);
	SetMember(L"LongText_Edit", (LONG_PTR)LongText_Edit);

	zyButton* LongText_BtnImport = new zyButton();
	LongText_BtnImport->创建(hWnd, L"导入文本(&I)");
	SetMember(L"LongText_BtnImport", (LONG_PTR)LongText_BtnImport);

	zyButton* LongText_BtnOk = new zyButton();
	LongText_BtnOk->创建(hWnd, L"确认(&O)");
	SetMember(L"LongText_BtnOk", (LONG_PTR)LongText_BtnOk);

	zyButton* LongText_BtnCancel = new zyButton();
	LongText_BtnCancel->创建(hWnd, L"取消(&C)");
	SetMember(L"LongText_BtnCancel", (LONG_PTR)LongText_BtnCancel);

	LongText_BtnOk->事件_被单击(TRegEventCallBack(this, &zyPropertyGrid::LongText_BtnOk_事件_被单击));
	LongText_BtnCancel->事件_被单击(TRegEventCallBack(this, &zyPropertyGrid::LongText_BtnCancel_事件_被单击));
	LongText_BtnImport->事件_被单击(TRegEventCallBack(this, &zyPropertyGrid::LongText_BtnImport_事件_被单击));
	PropertyGrid_item* pItem = (PropertyGrid_item*)form->GetMember(L"pItem");
	if (pItem) {
		LongText_Edit->置文本(pItem->Value);
	}
	return 0;
}
int DarkMoonUI::zyPropertyGrid::propertyItem_LongText_Dialog_OnSize(HWND hWnd, WORD cx, WORD cy) {
	zyEditBox* LongText_Edit = (zyEditBox*)GetMember(L"LongText_Edit");
	zyButton* LongText_BtnImport = (zyButton*)GetMember(L"LongText_BtnImport");
	zyButton* LongText_BtnOk = (zyButton*)GetMember(L"LongText_BtnOk");
	zyButton* LongText_BtnCancel = (zyButton*)GetMember(L"LongText_BtnCancel");
	LongText_Edit->移动(5, 5, cx - 10, cy - 40,0);
	LongText_BtnImport->moveWindow(5, cy - 40 + 10, 100, 24);
	LongText_BtnCancel->moveWindow(cx - 85, cy - 40 + 10, 80, 24);
	LongText_BtnOk->moveWindow(LongText_BtnCancel->左边() - 100 - 5, cy - 40 + 10, 80, 24);

	return 0;
}
int DarkMoonUI::zyPropertyGrid::LongText_BtnOk_事件_被单击(HWND hWnd, int ID) {
	zyDialogBox* LongText_Dialog = (zyDialogBox*)GetMember(L"LongText_Dialog");
	LongText_Dialog->返回值(IDOK);
	std::wstring* pStr = reinterpret_cast<std::wstring*>(LongText_Dialog->GetMember(L"EditText"));
	if (pStr)
		delete pStr;
	zyEditBox* LongText_Edit = (zyEditBox*)GetMember(L"LongText_Edit");
	LongText_Dialog->SetMember(L"EditText", (LONG_PTR)new std::wstring(LongText_Edit->取文本()));
	//delete LongText_Edit;
	//delete LongText_Dialog;
	LongText_Dialog->销毁();
	return 0;
}
int DarkMoonUI::zyPropertyGrid::LongText_BtnCancel_事件_被单击(HWND hWnd, int ID) {
	zyDialogBox* LongText_Dialog = (zyDialogBox*)GetMember(L"LongText_Dialog");
	LongText_Dialog->返回值(IDOK);
	LongText_Dialog->销毁();
	return 0;
}
int DarkMoonUI::zyPropertyGrid::LongText_BtnImport_事件_被单击(HWND hWnd, int ID) {
	zyDialogBox* LongText_Dialog = (zyDialogBox*)GetMember(L"LongText_Dialog");
	zyEditBox* LongText_Edit = (zyEditBox*)GetMember(L"LongText_Edit");


	return 0;
}

void DarkMoonUI::zyPropertyGrid::propertyItem_DrawBackAndLine(Gdiplus::Graphics* graphics, int nameWidth, int itemSelect, RECT rc, int isGroup) {
	// 获取背景颜色
	ARGB backColor = itemSelect != 0 ? (ARGB)GetMember(L"GridSelBackColor") : (ARGB)GetMember(L"GridBackColor");
	graphics->Clear(backColor);

	// 如果是非分组项，绘制白色背景
	if (isGroup == 0) {
		Gdiplus::SolidBrush brush(Color::White);
		graphics->FillRectangle(&brush, nameWidth, 0, rc.right - rc.left - nameWidth, rc.bottom - rc.top);
	}

	// 绘制灰色线条
	Gdiplus::Pen pen(Color::Gray);
	graphics->DrawLine(&pen, 0, rc.bottom - rc.top - 1, rc.right - rc.left, rc.bottom - rc.top - 1);

	// 如果是非分组项，绘制垂直线
	if (isGroup == 0) {
		graphics->DrawLine(&pen, nameWidth, 0, nameWidth, rc.bottom - rc.top);
	}
}
void DarkMoonUI::zyPropertyGrid::propertyItem_DrawExpanding(bool Open, HWND hWnd, HDC hdc, int left, int top, int right, int bottom) {
	// 尝试使用主题绘制扩展/收缩图标
	HTHEME hTheme = OpenThemeData(hWnd, L"TREEVIEW");
	if (hTheme != NULL) {
		RECT rc = { left + 2, top, left + 18, bottom };
		DrawThemeBackground(hTheme, hdc, TVP_GLYPH, Open ? GLPS_OPENED : GLPS_CLOSED, &rc, NULL);
		CloseThemeData(hTheme);
	}
	else {
		// 使用 GDI+ 绘制扩展/收缩图标
		Gdiplus::Graphics graphics(hdc);
		Gdiplus::Pen pen(Gdiplus::Color::Gray);

		int y = top + (bottom - top - 8) / 2;
		RECT rc = { left + 2, y, left + 2 + 8, y + 8 };

		// 绘制矩形
		graphics.DrawRectangle(&pen, rc.left, rc.top, 8, 8);

		// 绘制水平线
		graphics.DrawLine(&pen, (REAL)(rc.left + 2), (REAL)(rc.top + 4), (REAL)(rc.right - 2), (REAL)(rc.top + 4));

		// 如果未展开，绘制垂直线
		if (!Open) {
			graphics.DrawLine(&pen, (REAL)(rc.left + 4), (REAL)(rc.top + 2), (REAL)(rc.left + 4), (REAL)(rc.bottom - 2));
		}
	}
}
void DarkMoonUI::zyPropertyGrid::propertyItem_DrawNameAndStringValue(HWND hWnd, HDC hdc, PropertyGrid_item* pItem, Gdiplus::Graphics* graphics,
	int itemSelect, RECT rc, int nameWidth, int stringOffset, int stringFontStyle)
{
	// 线程安全的局部StringFormat（每次调用独立创建）
	Gdiplus::StringFormat format;
	format.SetLineAlignment(Gdiplus::StringAlignmentCenter);
	format.SetFormatFlags(Gdiplus::StringFormatFlagsNoWrap | Gdiplus::StringFormatFlagsMeasureTrailingSpaces);

	// 线程安全的临时画刷
	const ARGB textColor = itemSelect ?(ARGB)GetMember(L"GridSelTextColor") :(ARGB)pItem->TextColor;
	Gdiplus::SolidBrush textBrush(textColor);
	

	// 获取属性类型和层级
	const int type = pItem->Type;
	const int level = (type == 属性类型_分组) ? 0 : (pItem->Level - 1);

	// 创建字体对象（确保线程本地资源）
	HFONT hFont = (HFONT)SendMessage(hWnd, WM_GETFONT, 0, 0);
	Gdiplus::Font font(hdc, hFont);  // 直接使用栈对象避免动态分配

	// 绘制属性名称
	const Gdiplus::RectF nameRect((REAL)(level * 16 + 18),0.0f,(REAL)(rc.right - rc.left - 18),(REAL)(rc.bottom - rc.top));
	graphics->DrawString(pItem->Name.c_str(),-1,&font,nameRect,&format,&textBrush);

	// 绘制值区域背景
	if (type != 属性类型_分组) {
		Gdiplus::SolidBrush backBrush(Color::White);
		graphics->FillRectangle(&backBrush,(REAL)(nameWidth + 1),0.0f,(REAL)(rc.right - rc.left - nameWidth),(REAL)(rc.bottom - rc.top - 1));
	}
	

	/*std::vector<std::wstring>* pListString = (std::vector<std::wstring>*)GetMember(L"pListString");
	if (pListString && pListString->size() > 0) {
		int val = -1;
		std::wistringstream iss(pItem->Value);
		iss >> val;
		if (val!= -1 && val  < pListString->size())
			pItem->Value = (*pListString)[val];
	}*/
	// 绘制字符串值
	if (!pItem->Value.empty()) {
		// 增强：值区域有效性检查
		if (nameWidth <= 0 || rc.right - rc.left <= nameWidth) return;

		// 正确计算值区域坐标
		Gdiplus::RectF valueRect((REAL)(nameWidth + stringOffset + 5),0.0f,
			(REAL)(rc.right - rc.left - nameWidth - stringOffset - 5), // 防止越界
			(REAL)(rc.bottom - rc.top));
		if (stringFontStyle != 0) {
			Gdiplus::FontFamily fontFamily;
			font.GetFamily(&fontFamily);
			Gdiplus::Font styleFont(&fontFamily,font.GetSize(),stringFontStyle,font.GetUnit());
			graphics->DrawString(pItem->Value.c_str(),-1,&styleFont,valueRect,&format,&textBrush);
		}
		else {
			//Gdiplus::SolidBrush backBrush(Color::Coral);
			//graphics->FillRectangle(&backBrush, valueRect);
			graphics->DrawString(pItem->Value.c_str(),-1,&font,valueRect,&format,&textBrush);			
		}
	}
}
LRESULT DarkMoonUI::zyPropertyGrid::propertyItem_OnDraw(PropertyGrid_item* pItem, LPARAM lParam){
	// 实现代码
	if (pItem){
		NMLVCUSTOMDRAW* nmlv = reinterpret_cast<NMLVCUSTOMDRAW*>(lParam);
		const HWND hListView = nmlv->nmcd.hdr.hwndFrom; // 缓存列表控件句柄
		if (nmlv->nmcd.dwItemSpec >= (DWORD)SendMessage(hListView, LVM_GETITEMCOUNT, 0, 0))
			return CDRF_SKIPDEFAULT; // 防止绘制不存在的项
		// 合并获取矩形和列宽操作
		RECT rc = { 0 };
		const int nameWidth = (int)SendMessage(hListView, LVM_GETCOLUMNWIDTH, 0, 0);
		if (nameWidth <= 0 ||
			!SendMessage(hListView, LVM_GETITEMRECT, nmlv->nmcd.dwItemSpec, (LPARAM)&rc))
		{
			return CDRF_SKIPDEFAULT; // 无效数据直接返回
		}
		// 获取选择状态
		int isSelect = (int)SendMessage(hListView, LVM_GETITEMSTATE, nmlv->nmcd.dwItemSpec, LVIS_SELECTED);
		zyBuffGraphics buff;// = new zyBuffGraphics();
		int width = rc.right - rc.left;
		int height = rc.bottom - rc.top;
		if(!buff.Create(width, height)) return 0;

		Gdiplus::Graphics* g = buff.GetGraphics();
		const HDC hdcBuffer = buff.gethdc();
		// 绘制背景和线条 (优化内部资源重用)
		const bool hasChildren = pItem->Childs && !pItem->Childs->empty();
		propertyItem_DrawBackAndLine(g, nameWidth, isSelect, rc, hasChildren ? 1 : 0);

		// 绘制扩展按钮 (条件判断前置)
		if (hasChildren){
			const bool isExpanded = (pItem->State & 属性状态_扩展) != 0;
			const int indentLevel = pItem->Level * 16; // 缩进量计算
			propertyItem_DrawExpanding(isExpanded, hListView, hdcBuffer, indentLevel, 0, rc.right, height);
		}
		//std::wcout<< (HWND)GetMember(L"hWnd") <<L" = "<< pItem << std::endl;

		if (pItem->Type == 属性类型_列表) {
			std::vector<std::wstring>* pListString = (std::vector<std::wstring>*)GetMember(L"pListString");
			int val = (int)GetMember(L"itemindex");
			if (pListString && pListString->size() > 0 && val != -1) {
				pItem->Value = (*pListString)[val];
			}
		}
		propertyItem_DrawNameAndStringValue(hListView, hdcBuffer, pItem, g, isSelect, rc, nameWidth, 0, 0);
		//长文本按钮
		if (isSelect != 0 && (pItem->State & 属性状态_只读) == 0 && pItem->Type == 属性类型_长文本) {
			propertyItem_DrawButton(hListView, hdcBuffer, g, 属性类型_长文本, rc);
		}
		else if (isSelect != 0 && (pItem->State & 属性状态_只读) == 0 && pItem->Type == 属性类型_列表) {
			propertyItem_DrawButton(hListView, hdcBuffer, g, 属性类型_列表, rc);
		}
		buff.Copy2dc(nmlv->nmcd.hdc, rc.left, rc.top, width, height);

	}
	return 1;
}
void DarkMoonUI::zyPropertyGrid::propertyItem_DrawButton(HWND hWnd, HDC hdc, Gdiplus::Graphics* graphics,int type, RECT rc) {
	RECT rc2 = rc;
	int height = rc2.bottom - rc2.top;
	rc2.left = rc2.right - height - 1;
	rc2.top = 0;
	rc2.bottom = height - 1;

	Gdiplus::SolidBrush backBrush(Color::White);
	graphics->FillRectangle(&backBrush, (int)(rc2.left - 2), rc2.top, (int)(rc2.right - rc2.left + 2), (int)(rc2.bottom - rc2.top));
	backBrush.SetColor(Color::Black);
	if (type == 属性类型_长文本) {
		HTHEME hTheme = OpenThemeData(hWnd,L"Button");
		if (hTheme == 0) {
			DrawFrameControl(hdc, &rc2, DFC_BUTTON, DFCS_BUTTONPUSH | 0);
		}
		else {
			DrawThemeBackground(hTheme, hdc, BP_PUSHBUTTON, PBS_NORMAL, &rc2, 0);
			CloseThemeData(hTheme);
		}
		graphics->FillEllipse(&backBrush, (int)(rc2.left +(height - 12) / 2 + 1), (int)(height / 2 - 1), 3, 3);
		graphics->FillEllipse(&backBrush, (int)(rc2.left +(height - 12) / 2 + 5), (int)(height / 2 - 1), 3, 3);
		graphics->FillEllipse(&backBrush, (int)(rc2.left + (height - 12) / 2 + 9), (int)(height / 2 - 1), 3, 3);

	}else if (type == 属性类型_列表) {
		HTHEME hTheme = OpenThemeData(hWnd, L"COMBOBOX");
		if (hTheme == 0) {
			DrawFrameControl(hdc, &rc2, DFC_BUTTON, DFCS_BUTTONPUSH | 0);
			PointF points[3] = {
				{ (REAL)(rc2.left + (height - 12) / 2 ), (REAL)(height / 2 - 2) },
				{ (REAL)(rc2.left + (height - 12) / 2 + 10), (REAL)(height / 2 - 2) },
				{ (REAL)(rc2.left + (height - 12) / 2 + 5), (REAL)(height / 2 - 2 + 5) }
			};
			graphics->FillPolygon(&backBrush, points,3);
		}
		else {
			DrawThemeBackground(hTheme, hdc, CP_DROPDOWNBUTTON, CBXS_NORMAL, &rc2, 0);
			CloseThemeData(hTheme);
		}
	}

}
void DarkMoonUI::zyPropertyGrid::propertyItem_ShowList(PropertyGrid_item* pItem, int index) {
	HWND hwnd = (HWND)GetMember(L"hWnd");
	zyWidget* pListWindow = (zyWidget*)GetMember(L"pListWindow");
	zyListBox* pList = (zyListBox*)GetMember(L"pList");
	if (pListWindow != nullptr && pList != nullptr) {
		if (pList->取属性(L"NoShow") != 0) {
			pList->置属性(L"NoShow", 0);
			return;
		}
		if (pListWindow->是否可视() != false && pList->取属性(L"NoShow") == 0) {
			pList->置属性(L"pItem", pItem);
			pList->清空();
			int val = 0;
			std::wistringstream iss(pItem->Value);
			iss >> val;
			std::vector<std::wstring>* pListString = (std::vector<std::wstring>*)GetMember(L"pListString");
			std::vector<LPARAM>* pListlParam = (std::vector<LPARAM>*)GetMember(L"pListlParam");
			if (!pListString->empty() && !pListlParam->empty()) {
				int count = (int)pListString->size();
				for (int i = 0; i < count; i++) {
					pList->加入项目(pListString->at(i), (int)pListlParam->at(i));
				}
				RECT itemRect = pList->取项目矩形(count - 1);
				int itemHeight = (itemRect.bottom - itemRect.top) * count + 5;
				if (itemHeight > 200) itemHeight = 200;
				RECT rc1 = { 0 };
				SystemParametersInfo(SPI_GETWORKAREA, 0, &rc1, 0);
				int screenHeight = rc1.bottom;
				RECT rc = { 0 };
				rc.top = 1;
				SendMessage(hwnd, LVM_GETSUBITEMRECT, index, (LPARAM) &rc);
				POINT pt = { rc.left , rc.top };
				ClientToScreen(hwnd, &pt);
				if (rc.bottom + itemHeight > screenHeight)
					pListWindow->moveWindow(rc.left, rc.top - itemHeight, rc.right - rc.left, itemHeight);
				else {
					pListWindow->moveWindow(pt.x, pt.y + 20 , rc.right - rc.left, itemHeight);
				}
				pList->moveWindow(0, 0, pListWindow->宽度(), pListWindow->高度());
				zyForm* topWindow = new zyForm;
				topWindow->_BindWindow(GetAncestor(hwnd, GA_ROOT));
				SetMember(L"oldKeepTitle", topWindow->保持标题栏激活());
				topWindow->保持标题栏激活(true);
				pListWindow->可视(true);
				pList->获取焦点();
				pList->置现行选中项(val);
			}
		}
		else
			zyPropretyGrid_HideAll();
	}
}
int DarkMoonUI::zyPropertyGrid::propertyItem_ShowEdit(PropertyGrid_item* pItem, int iItem, int offsetLeft, int offsetRight) {
	zyEditBox* pEdit = (zyEditBox*)GetMember(L"pEdit");
	if(!pEdit) return 0;
	HWND hWndListView = (HWND)GetMember(L"hWnd");
	HWND hWndEdit = (HWND)pEdit->GetMember(L"hWnd");
	if (IsWindow(hWndListView) != 0 && IsWindowVisible(hWndEdit) == 0) {
		pEdit->置属性(L"pItem", pItem);
		pEdit->背景色(RGB(255, 255, 255));
		RECT rc= { 0 };
		rc.top = 1;
		SendMessage(hWndListView, LVM_GETSUBITEMRECT, iItem, (LPARAM) & rc);
		pEdit->移动(rc.left + 4 + offsetLeft, rc.top + 2, rc.right - rc.left - 8 - offsetRight - offsetLeft, rc.bottom - rc.top - 3, true);
		if (!pItem->Value.empty())
			pEdit->置文本(pItem->Value);
		pEdit->置内容改变标志(false);
		pEdit->可视(true);

		return 1;
	}
	return 0;
}
int DarkMoonUI::zyPropertyGrid::propertyItem_BeginEdit(PropertyGrid_item* pItem, int index) {
	// 实现代码
	zyPropretyGrid_HideAll();
	// 添加类型和状态检查
	if (pItem->Type == 属性类型_分组 || (pItem->State & 属性状态_只读) != 0)
		return 1;
	TRegEvent* p = GetRegEvent(L"OnBeginEdit");
	HWND hWnd = (HWND)GetMember(L"hWnd");
	if (p) {
		int ret = p->handler<int>(hWnd, pItem);
		if (ret != 0)
			return 1;
	}
	if(pItem->Type == 属性类型_文本)
		return propertyItem_ShowEdit(pItem, index, 0, 0);
	else if (pItem->Type == 属性类型_长文本) {
		RECT rc = { 0 };
		rc.top = 1;
		SendMessage(hWnd, LVM_GETSUBITEMRECT, index, (LPARAM) & rc);
		return propertyItem_ShowEdit(pItem, index, 0, rc.bottom - rc.top + 2);
	}
	else if (pItem->Type == 属性类型_列表) {
		TRegEvent* p = GetRegEvent(L"OnBeginEdit");
		if (p) {
			int ret = p->handler<int>(GetMember(L"hWnd"), pItem);
			if (ret != 0)
				return 1;
		}
	}
	
	return 0;
}
int DarkMoonUI::zyPropertyGrid::propertyItem_EndEdit(HANDLE grid, HANDLE pItem, int index) {
	// 实现代码
	PropertyGrid_item* pItem1 = (PropertyGrid_item*)pItem;
	if (pItem1->Type == 属性类型_分组 || (pItem1->State & 属性状态_只读) != 0)
		return 1;
	
	if (pItem1->Type == 属性类型_文本 || pItem1->Type == 属性类型_长文本) {
		zyEditBox* pEdit = (zyEditBox*)GetMember(L"pEdit");
		if (!pEdit) return 0;
		if (pEdit->内容是否改变()) {
			std::wstring pStr = pEdit->取文本();

			if (pItem1)
				pItem1->Value = pStr;
			TRegEvent* p = (TRegEvent*)GetRegEvent(L"OnValueChange");
			if (p) {
				p->handler<int>(GetMember(L"hWnd"), pItem);
			}
			return 1;
		}
	}else if (pItem1->Type == 属性类型_列表) {
		SetMember(L"itemindex", index);
		HWND hwnd = (HWND)GetMember(L"hWnd");
		int listIndex = (int)SendMessage(hwnd, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
		SendMessage(hwnd, LVM_REDRAWITEMS, listIndex, listIndex);
		TRegEvent* p = (TRegEvent*)GetRegEvent(L"OnValueChange");
		if (p) {
			p->handler<int>(hwnd, pItem);
		}
	}
	return 0;
}
int DarkMoonUI::zyPropertyGrid::propertyItem_OnClick(PropertyGrid_item* pItem,int index,int isOnButton,int mouseState) {
	if (pItem->Type == 属性类型_分组 || (pItem->State & 属性状态_只读) != 0) {
		// 实现代码
		TRegEvent* p = GetRegEvent(L"OnClick");
		if (p) {
			int ret = p->handler<int>(GetMember(L"hWnd"), pItem, isOnButton);
			if (ret != 0)
				return 1;
		}
		zyPropretyGrid_ExpandChange(pItem, index);

		return 1;
	}
	else if (pItem->Type == 属性类型_文本){
		if((pItem->State & 属性状态_只读) != 0) return 1;
		return propertyItem_BeginEdit(pItem, index);
	}
	else if (pItem->Type == 属性类型_长文本) {
		if ((pItem->State & 属性状态_只读) != 0) return 1;
		HWND hwnd = (HWND)GetMember(L"hWnd");
		if (isOnButton == 0)
			propertyItem_BeginEdit(pItem, index);
		else {
			int state = (int)SendMessage(hwnd, LVM_GETITEMSTATE, index, LVIS_SELECTED);
			if (state == LVIS_SELECTED) {
				zyPropretyGrid_HideAll();
				TRegEvent* p = GetRegEvent(L"OnClick");
				if (p) {
					int ret = p->handler<int>(hwnd, pItem, isOnButton);
					if (ret != 0)
						return 1;
				}
				zyDialogBox* LongText_Dialog = new zyDialogBox();
				LongText_Dialog->边框(窗口边框_普通可调边框);
				LongText_Dialog->SetMember(L"pItem", (LONG_PTR)pItem);
				LongText_Dialog->事件_创建完毕(TRegEventCallBack(this,&zyPropertyGrid::propertyItem_LongText_Dialog_OnCraete));
				LongText_Dialog->事件_尺寸被改变(TRegEventCallBack(this, &zyPropertyGrid::propertyItem_LongText_Dialog_OnSize));
				LongText_Dialog->置开始位置(开始位置_居中屏幕);
				LongText_Dialog->Esc关闭(true);
				SetMember(L"LongText_Dialog",(LONG_PTR)LongText_Dialog);
				int ret = (int)LongText_Dialog->载入(GetAncestor(hwnd, GA_ROOT), L"请输入文本：", POINT{ 0, 0 }, POINT{ 250, 180 },
					WS_VISIBLE | WS_MAXIMIZEBOX | WS_DLGFRAME, WS_EX_DLGMODALFRAME, true);
				if (ret == IDOK) {
					LONG_PTR pEditText = LongText_Dialog->GetMember(L"EditText");
					if (pEditText) {
						std::wstring* pEditText_str = reinterpret_cast<std::wstring*>(pEditText);
						pItem->Value = *pEditText_str;
						delete pEditText_str; // 释放内存
						LongText_Dialog->SetMember(L"EditText",0);
					}
					SendMessage(hwnd, LVM_REDRAWITEMS, index, index);
					TRegEvent* p = GetRegEvent(L"OnValueChange");
					if (p) {
						p->handler<int>(hwnd, pItem);
					}
					//return 1;
				}
				//销毁对话框后释放内存

				std::vector<std::wstring>* pListString = (std::vector<std::wstring>*)GetMember(L"pListString");
				std::vector<LPARAM>* pListlParam = (std::vector<LPARAM>*)GetMember(L"pListlParam");
				if (pListString) {
					for (int i = 0; i < pListString->size(); i++) {
						pListString->clear();
					}
					delete pListString;
				}
				if (pListlParam) {
					for (int i = 0; i < pListlParam->size(); i++) {
						pListlParam->clear();
					}
					delete pListlParam;
				}
				zyEditBox* LongText_Edit = (zyEditBox*)GetMember(L"LongText_Edit");
				if(LongText_Edit)
					delete LongText_Edit;
				zyButton* LongText_BtnImport = (zyButton*)GetMember(L"LongText_BtnImport");
				if (LongText_BtnImport)
					delete LongText_BtnImport;
				zyButton* LongText_BtnCancel = (zyButton*)GetMember(L"LongText_BtnCancel");
				if (LongText_BtnCancel)
					delete LongText_BtnCancel;
				zyButton* LongText_BtnOk = (zyButton*)GetMember(L"LongText_BtnOk");
				if (LongText_BtnOk)
					delete LongText_BtnOk;
				delete LongText_Dialog;
				return 1;
			}
		}
	}
	else if (pItem->Type == 属性类型_列表) {
		if ((pItem->State & 属性状态_只读) != 0) return 1;
		HWND hwnd = (HWND)GetMember(L"hWnd");
		int selected = (int)SendMessage(hwnd, LVM_GETITEMSTATE, index, LVIS_SELECTED);
		if (mouseState == 0 || mouseState == 1) {
			if (selected != LVIS_SELECTED) {
				zyPropretyGrid_HideAll();
			}
			else {
				TRegEvent* p = GetRegEvent(L"OnClick");
				if (p) {
					int ret = p->handler<int>(hwnd, pItem, isOnButton);
					if (ret != 0)
						return 1;
				}
				propertyItem_ShowList(pItem, index);
			}
		}
	}
	return 0;
}

void DarkMoonUI::zyPropertyGrid::_init(HWND hWnd) {
	// 清理旧资源
	if (zyLmageList* pOldImgList = reinterpret_cast<zyLmageList*>(GetMember(L"pImageList"))) {
		pOldImgList->getObject()->release();
	}
	if (zyEditBox* pOldEdit = reinterpret_cast<zyEditBox*>(GetMember(L"pEdit"))) {
		delete pOldEdit;
	}
	if (zyWidget* pOldListWin = reinterpret_cast<zyWidget*>(GetMember(L"pListWindow"))) {
		delete pOldListWin;
	}
	if (zyListBox* pOldList = reinterpret_cast<zyListBox*>(GetMember(L"pList"))) {
		delete pOldList;
	}
	if (auto* pOldProps = reinterpret_cast<std::vector<PropertyGrid_item*>*>(GetMember(L"Propertys"))) {
		delete pOldProps;
	}

	// 初始化样式属性
	constexpr int rowHeight = 20;
	SetMember(L"GridBackColor", zyRGB2ARGB(14933712, 255));
	SetMember(L"GridTextColor", 0x000000L);
	SetMember(L"GridSelBackColor", zyRGB2ARGB(GetSysColor(COLOR_HIGHLIGHT), 255));
	SetMember(L"GridSelTextColor", zyRGB2ARGB(0, 255));

	// 创建行高图片组
	zyLmageList* pImageList = new zyLmageList();
	pImageList->Create(rowHeight, rowHeight);
	pImageList->getObject()->addRef();
	SetMember(L"pImageList", reinterpret_cast<LONG_PTR>(pImageList));
	SendMessage(hWnd, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)pImageList->取句柄());

	// 配置列表控件
	SendMessage(hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
		LVS_EX_SUBITEMIMAGES | LVS_EX_INFOTIP | LVS_EX_DOUBLEBUFFER | LVS_EX_FULLROWSELECT);
	SetMember(L"Propertys", reinterpret_cast<LONG_PTR>(new std::vector<PropertyGrid_item*>()));

	// 清空列表数据
	SendMessage(hWnd, LVM_DELETEALLITEMS, 0, 0);
	while (SendMessage(hWnd, LVM_DELETECOLUMN, 0, 0) != FALSE) {} // 更安全的列删除方式

	// 插入双列
	LVCOLUMN col = { LVCF_FMT | LVCF_WIDTH };
	col.fmt = LVCFMT_LEFT;
	col.cx = 100;
	//col.pszText = L"属性";
	SendMessage(hWnd, LVM_INSERTCOLUMNW, 0, reinterpret_cast<LPARAM>(&col));
	//col.pszText = L"值";
	SendMessage(hWnd, LVM_INSERTCOLUMNW, 1, reinterpret_cast<LPARAM>(&col));

	// 创建编辑控件
	zyEditBox* pEdit = new zyEditBox();
	pEdit->边框(控件边框_无边框);
	pEdit->通用事件_窗口消息(TRegEventCallBack(this, &zyPropertyGrid::propertyItem_Edit_Msg));
	pEdit->添加ZY窗口风格(ZY窗口风格_需要回车键);
	pEdit->垂直居中(TRUE,0);
	pEdit->创建(hWnd, L"", POINT{ 0, 0 }, POINT{ 100, rowHeight + 8 }, 0, ES_AUTOHSCROLL | WS_TABSTOP);
	pEdit->置属性(L"pListView", this);
	pEdit->可视(FALSE);
	pEdit->getObject()->addRef();
	SetMember(L"pEdit", reinterpret_cast<LONG_PTR>(pEdit));

	// 创建下拉容器
	zyWidget* pListWindow = new zyWidget();
	pListWindow->边框(窗口边框_无边框);
	//pListWindow->通用事件_窗口消息(TRegEventCallBack(this, &zyPropertyGrid::propertyItem_ListWindow_Msg));
	pListWindow->创建(GetAncestor(hWnd, GA_ROOT), L"", POINT{ 0, 0 }, POINT{ 100, 100 }, WS_POPUP);
	pListWindow->置属性(L"pListView", this);
	pListWindow->getObject()->addRef();
	SetMember(L"pListWindow", reinterpret_cast<LONG_PTR>(pListWindow));

	// 创建列表框
	zyListBox* pList = new zyListBox();
	pList->通用事件_窗口消息(TRegEventCallBack(this, &zyPropertyGrid::propertyItem_list_Msg));
	pList->事件_列表项被选择(TRegEventCallBack(this, &zyPropertyGrid::propertyItem_List_OnSelected));
	pList->创建(pListWindow->取窗口句柄());
	pList->置属性(L"pListView", this);
	pList->getObject()->addRef();
	SetMember(L"pList", reinterpret_cast<LONG_PTR>(pList));

	// 触发布局更新
	RECT rc;
	GetClientRect(hWnd, &rc);
	SendMessage(hWnd, WM_SIZE, 0, MAKEWPARAM(rc.right - rc.left, rc.bottom - rc.top));
}
DarkMoonUI::PropertyGrid_item* DarkMoonUI::zyPropertyGrid::_InsertProperty(int index, PropertyGrid_item* parent, const std::wstring& name, int type, 
	int state, LPARAM lParam,const std::wstring& Value) {
	// 获取属性列表
	std::vector<PropertyGrid_item*>* Propertys = (std::vector<PropertyGrid_item*>*)(GetMember(L"Propertys"));
	if (!Propertys) return nullptr;

	int parentState = 0;
	int parentLevel = 0;

	// 处理父项逻辑
	if (parent) {
		parentState = parent->State;
		parentLevel = parent->Level + 1; // 子项层级 = 父项层级 + 1

		// 如果父项没有子项容器，则创建
		if (!parent->Childs) {
			parent->Childs = new std::vector<PropertyGrid_item*>();
		}
		Propertys = parent->Childs; // 切换到父项的子项列表
	}

	// 处理插入位置
	int count = static_cast<int>(Propertys->size());
	if (index < 0 || index > count) index = count;

	// 创建新属性项
	PropertyGrid_item* pitem = new PropertyGrid_item();
	pitem->Name = name;
	pitem->Type = type;
	pitem->State = state;
	pitem->TextColor = zyRGB2ARGB(0, 255); // 黑色
	pitem->Level = parentLevel;
	pitem->lParam = lParam;
	pitem->Parent = parent;
	pitem->Childs = new std::vector<PropertyGrid_item*>(); // 初始化子项容器
	pitem->Value = Value;
	// 插入到属性列表
	Propertys->insert(Propertys->begin() + index, pitem);

	// 获取列表视图句柄
	HWND hWnd = (HWND)(GetMember(L"hWnd"));

	// 处理顶层项插入逻辑
	if (!parent) {
		if (index > 0) {
			// 查找所有顶层项
			std::vector<int> topItems;
			int itemCount = static_cast<int>(SendMessage(hWnd, LVM_GETITEMCOUNT, 0, 0));
			for (int i = 0; i < itemCount; i++) {
				LVITEM item = { 0 };
				item.mask = LVIF_PARAM;
				item.iItem = i;
				if (SendMessage(hWnd, LVM_GETITEM, 0, reinterpret_cast<LPARAM>(&item))) {
					PropertyGrid_item* currentItem = reinterpret_cast<PropertyGrid_item*>(item.lParam);
					if (currentItem && !currentItem->Parent) {
						topItems.push_back(i); // 记录顶层项索引
					}
				}
			}

			// 计算实际插入位置
			count = static_cast<int>(topItems.size());
			if (index >= count) {
				// 插入到最后一个顶层项之后
				int lastTopIndex = topItems[count - 1];
				LVITEM item = { 0 };
				item.mask = LVIF_PARAM;
				item.iItem = lastTopIndex;
				if (SendMessage(hWnd, LVM_GETITEM, 0, reinterpret_cast<LPARAM>(&item))) {
					PropertyGrid_item* lastTopItem = reinterpret_cast<PropertyGrid_item*>(item.lParam);
					if (lastTopItem && (lastTopItem->State & 属性状态_扩展)) {
						// 如果最后一个顶层项是展开状态，插入到其子项之后
						int childCount = static_cast<int>(lastTopItem->Childs->size());
						index = lastTopIndex + childCount + 1;
					}
					else {
						// 否则插入到最后一个顶层项之后
						index = lastTopIndex + 1;
					}
				}
			}
			else {
				// 插入到指定顶层项的位置
				index = topItems[index];
			}
		}

		// 插入到列表视图
		LVITEM lvitem = { 0 };//LVIF_STATE 表示state成员有效 鼠标移动时会触发，否则可能触发不及时导致绘制不完整
		lvitem.mask = LVIF_PARAM | LVIF_TEXT | LVIF_STATE ;
		lvitem.iItem = index;
		lvitem.iSubItem = 0;
		lvitem.pszText = const_cast<LPWSTR>(pitem->Name.c_str());
		lvitem.cchTextMax = static_cast<int>(pitem->Name.size());
		//lvitem.iIndent = parentLevel;//LVIF_INDENT  表示iIndent成员有效 主要用于设置列表项的缩进级别
		lvitem.lParam = reinterpret_cast<LPARAM>(pitem);

		if (SendMessage(hWnd, LVM_INSERTITEM, 0, reinterpret_cast<LPARAM>(&lvitem)) == -1) {
			delete pitem; // 插入失败，释放内存
			return nullptr;
		}
	}
	// 处理子项插入逻辑
	else if (parentState & 属性状态_扩展) {
		// 查找父项在列表中的位置
		LVFINDINFO lvfi = { 0 };
		lvfi.flags = LVFI_PARAM;
		lvfi.lParam = reinterpret_cast<LPARAM>(parent);
		int parentIndex = static_cast<int>(SendMessage(hWnd, LVM_FINDITEM, -1, reinterpret_cast<LPARAM>(&lvfi)));
		if (parentIndex == -1) return pitem; // 父项未找到，直接返回

		// 计算子项插入位置
		index = parentIndex + index + 1;

		// 插入到列表视图
		LVITEM lvitem = { 0 };//LVIF_STATE 表示state成员有效 鼠标移动时会触发，否则可能触发不及时导致绘制不完整
		lvitem.mask = LVIF_PARAM | LVIF_TEXT | LVIF_STATE ;
		lvitem.iItem = index;
		lvitem.iSubItem = 0;
		lvitem.pszText = const_cast<LPWSTR>(pitem->Name.c_str());
		lvitem.cchTextMax = static_cast<int>(pitem->Name.size());
		//lvitem.iIndent = parentLevel;//LVIF_INDENT  表示iIndent成员有效 主要用于设置列表项的缩进级别
		lvitem.lParam = reinterpret_cast<LPARAM>(pitem);

		if (SendMessage(hWnd, LVM_INSERTITEM, 0, reinterpret_cast<LPARAM>(&lvitem)) == -1) {
			delete pitem; // 插入失败，释放内存
			return nullptr;
		}
	}

	return pitem;
}
BOOL DarkMoonUI::zyPropertyGrid::创建(HWND 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, 
	int 参数_扩展风格)
{
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();
	if (参数_扩展风格 == 0)
		参数_扩展风格 = WS_EX_CLIENTEDGE;
	bool b = _CreateCtrol(参数_父窗口, 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 | LVS_NOCOLUMNHEADER | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS, 参数_扩展风格, L"SysListView32");
	if (b) {
		_init(getHwnd());
	}
	return b;
}
BOOL DarkMoonUI::zyPropertyGrid::创建(zyWindow& 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, 
	int 参数_扩展风格)
{
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();
	if (参数_扩展风格 == 0)
		参数_扩展风格 = WS_EX_CLIENTEDGE;
	bool b = _CreateCtrol(参数_父窗口.getHwnd(), 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 | LVS_NOCOLUMNHEADER | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS, 参数_扩展风格, L"SysListView32");
	if (b) {
		_init(getHwnd());
	}
	return b;
}
bool DarkMoonUI::zyPropertyGrid::绑定(HWND 参数_父窗口, int 参数_资源ID, bool 参数_使用默认字体) {//成功返回真，失败返回假。
	HWND hwnd = GetDlgItem(参数_父窗口, 参数_资源ID);
	if (hwnd != 0) {
		int Style = (int)GetWindowLongPtr(hwnd, GWL_STYLE);
		Style = Style & ~(LVS_SMALLICON | LVS_LIST | LVS_AUTOARRANGE | LVS_EDITLABELS | LVS_NOSCROLL);
		Style = Style | LVS_NOCOLUMNHEADER | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS;
		SetWindowLongPtr(hwnd, GWL_STYLE, Style);
		_init(hwnd);
		if (hwnd != 0 && 参数_使用默认字体) {
			SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
		}
	}
	return _BindWindow(hwnd);
}
bool DarkMoonUI::zyPropertyGrid::绑定窗口句柄(HWND 参数_窗口句柄) {//成功返回真，失败返回假。
	if (IsWindow(参数_窗口句柄) != 0) {
		int Style = (int)GetWindowLongPtr(参数_窗口句柄, GWL_STYLE);
		Style = Style & ~(LVS_SMALLICON | LVS_LIST | LVS_AUTOARRANGE | LVS_EDITLABELS | LVS_NOSCROLL);
		Style = Style | LVS_NOCOLUMNHEADER | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS;
		SetWindowLongPtr(参数_窗口句柄, GWL_STYLE, Style);
		_init(参数_窗口句柄);
	}
	return _BindWindow(参数_窗口句柄);
}
DarkMoonUI::PropertyGrid_item* DarkMoonUI::zyPropertyGrid::插入项目_分组(int 参数_索引, const std::wstring& 参数_名称, LPARAM 参数_项目数值) {//成功返回项目句柄，失败返回0
	if (参数_索引 == 0) {
		参数_索引 = -1;
	}
	PropertyGrid_item* pItem = _InsertProperty(参数_索引, 0, 参数_名称, 属性类型_分组, 属性状态_扩展, 参数_项目数值, L"");
	if (pItem == nullptr) {
		return 0;
	}
	//int value = propertyItem_Group_New (pItem);
	//if (value == 0) {
		//zyPropretyGrid_FreePropertyItem (pItem);
	//	return 0;
	//}
	//pItem->Value = value;
	return pItem;
}
void* DarkMoonUI::zyPropertyGrid::插入项目_文本(int 参数_索引, PropertyGrid_item* 参数_父项目句柄 ,const std::wstring& 参数_名称, const std::wstring& 参数_值, 
	LPARAM 参数_项目数值) {//成功返回项目句柄，失败返回0
	if (参数_索引 == 0) {
		参数_索引 = -1;
	}
	PropertyGrid_item* pItem = _InsertProperty(参数_索引, 参数_父项目句柄, 参数_名称, 属性类型_文本, 属性状态_收缩, 参数_项目数值, 参数_值);
	if (pItem == nullptr) {
		return 0;
	}
	return pItem;
}
void* DarkMoonUI::zyPropertyGrid::插入项目_长文本(int 参数_索引, PropertyGrid_item* 参数_父项目句柄, const std::wstring& 参数_名称, const std::wstring& 参数_值,
	LPARAM 参数_项目数值) {//成功返回项目句柄，失败返回0
	if (参数_索引 == 0) {
		参数_索引 = -1;
	}
	PropertyGrid_item* pItem = _InsertProperty(参数_索引, 参数_父项目句柄, 参数_名称, 属性类型_长文本, 属性状态_收缩, 参数_项目数值, 参数_值);
	if (pItem == nullptr) {
		return 0;
	}
	return pItem;
}
DarkMoonUI::PropertyGrid_item* DarkMoonUI::zyPropertyGrid::插入项目_列表(int 参数_索引, PropertyGrid_item* 参数_父项目句柄, const std::wstring& 参数_名称, 
	int 参数_值, LPARAM 参数_项目数值) {//成功返回项目句柄，失败返回0
	if (参数_索引 == 0) {
		参数_索引 = -1;
	}
	std::wstring wstr = std::to_wstring(参数_值);
	PropertyGrid_item* pItem = _InsertProperty(参数_索引, 参数_父项目句柄, 参数_名称, 属性类型_列表, 属性状态_收缩, 参数_项目数值, wstr);
	if (pItem == nullptr) {
		return 0;
	}
	std::vector<std::wstring>* pListString = new std::vector<std::wstring>();
	std::vector<LPARAM>* pListlParam = new std::vector<LPARAM>();
	SetMember(L"pListString", (LONG_PTR)pListString);
	SetMember(L"pListlParam", (LONG_PTR)pListlParam);
	return pItem;
}
int DarkMoonUI::zyPropertyGrid::列表_添加项目(PropertyGrid_item* 参数_父项目句柄, const std::wstring& 参数_名称, LPARAM 参数_项目数值) {//成功返回项目句柄，失败返回0
	if (参数_父项目句柄 == nullptr) {
		return -1;
	}
	if(参数_父项目句柄->Type != 属性类型_列表) {
		return -1;
	}
	std::vector<std::wstring>* pListString = (std::vector<std::wstring>*)GetMember(L"pListString");
	std::vector<LPARAM>* pListlParam = (std::vector<LPARAM>*)GetMember(L"pListlParam");
	pListString->push_back(参数_名称);
	pListlParam->push_back(参数_项目数值);
	return (int)pListlParam->size();
}

